/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.utils;

import java.util.*;
import java.io.*;

public class VectorFile {
	private Vector store=new Vector();

	public VectorFile() {}

	public VectorFile(File files[]) {
		for (int i =0;i<files.length;i++) {
			add(files[i]);
		}
	}

	public File get(int pos){
		return (File)store.get(pos);
	}

	public void add(File item){
		store.add(item);
	}

	public boolean remove(File item){
		return store.remove(item);
	}

	public File remove(int pos){
		return (File) store.remove(pos);
	}

	public int size(){
		return store.size();
	}

	public int indexOf(String filename) {
		for (int i =0;i < size();i++) {
			if (get(i).getName().equals(filename)) {
				return i;
			}
		}
		return -1;
	}

	public int indexOf(File file) {
		return indexOf(file.getName());
	}

	public boolean contains(String filename) {
		return indexOf(filename) != -1;
	}

	public boolean contains(File file) {
		return indexOf(file) != -1;
	}

	public File getLastModified() {
		int index = -1;
		long last_mod = Long.MAX_VALUE;
		for (int i =0;i< size();i++) {
			if (get(i).lastModified() < last_mod) {
				index = i;
				last_mod = get(i).lastModified();
			}
		}
		if (index != -1) {
			return get(index);
		}
		return null;
	}

	public File [] toArray() {
		File file[] = new File[size()];
		for (int i = 0;i < size();i++) {
			file[i]= get(i);
		}
		return file;
	}
	
	public boolean delete() {
		boolean deleted = true;
		for (int i = 0;i < size();i++) {
			if (!get(i).delete()) {
				deleted = false;
			}
		}
		return deleted;
	}
	
	public void deleteOnExit() {
		for (int i = 0;i < size();i++) {
			get(i).deleteOnExit();
		}
	}
}

